//
//  PayContentViewController.m
//  pingtaiceshi
//
//  Created by longziyuan on 14-8-27.
//
//

#import "PayContentViewController.h"
#import "GlobalVar.h"

#import "iToast.h"

@interface PayContentViewController ()

@end

@implementation PayContentViewController
@synthesize ChongZhi_Type;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    
   
    
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderWidth = 1;
    
    bgView.layer.borderColor=[UIColor blackColor].CGColor;
    
    
    int KH_Length = 0;
    int MM_Length = 0;
    NSString *Name = @"";
    
    
    if(ChongZhi_Type == 0){
        KH_Length = 17;
        MM_Length = 18;
        Name = @"中国移动";
        self.title = @"中国移动充值";
    }
    if(ChongZhi_Type == 1){
        KH_Length = 15;
        MM_Length = 19;
        Name = @"中国联通";
        self.title = @"中国联通充值";
    }
    
    if(ChongZhi_Type == 3){
        KH_Length = 19;
        MM_Length = 18;
        Name = @"中国电信";
        self.title = @"中国电信充值";
    }
    
    bgScroll=[[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_W, IPHONE_H)]autorelease];
    bgScroll.showsVerticalScrollIndicator=NO;
    bgScroll.showsHorizontalScrollIndicator=YES;
    bgScroll.userInteractionEnabled=YES;
    [self.view addSubview:bgScroll];
    bgView=[[[UIView alloc]initWithFrame:CGRectMake(7, 7, 306, 445)]autorelease];
    [bgScroll addSubview:bgView];
    
    label1=[[[UILabel alloc]initWithFrame:CGRectMake(18, 17, 93, 21)]autorelease];
    label1.backgroundColor=[UIColor clearColor];
    label1.text=@"充值卡卡号:";
    [bgView addSubview:label1];
    
    textField1=[[[UITextField alloc]initWithFrame:CGRectMake(20, 46, 266, 30)]autorelease];
    textField1.backgroundColor=[UIColor clearColor];
    textField1.delegate=self;
    textField1.borderStyle=UITextBorderStyleRoundedRect;
    textField1.layer.borderColor=[UIColor blackColor].CGColor;
    textField1.font=[UIFont systemFontOfSize:14.0];
    textField1.keyboardType=UIKeyboardTypeNumberPad;
    textField1.returnKeyType=UIReturnKeyDone;
    textField1.backgroundColor=[UIColor whiteColor];
    [bgView addSubview:textField1];

    label2=[[[UILabel alloc]initWithFrame:CGRectMake(18, 92, 93, 21)]autorelease];
    label2.backgroundColor=[UIColor clearColor];
    label2.text=@"充值卡密码:";
    [bgView addSubview:label2];
    
    textField2=[[[UITextField alloc]initWithFrame:CGRectMake(20, 121, 266, 30)]autorelease];
    textField2.backgroundColor=[UIColor clearColor];
    textField2.delegate=self;
    textField2.borderStyle=UITextBorderStyleRoundedRect;
    textField2.layer.borderColor=[UIColor blackColor].CGColor;
    textField2.font=[UIFont systemFontOfSize:14.0];
    textField2.keyboardType=UIKeyboardTypeNumberPad;
    textField2.returnKeyType=UIReturnKeyDone;
    textField2.backgroundColor=[UIColor whiteColor];
    [bgView addSubview:textField2];

    label3=[[[UILabel alloc]initWithFrame:CGRectMake(18, 167, 93, 21)]autorelease];
    label3.backgroundColor=[UIColor clearColor];
    label3.text=@"充值卡金额:";
    [bgView addSubview:label3];
    
    textField3=[[[UITextField alloc]initWithFrame:CGRectMake(20, 196, 266, 30)]autorelease];
    textField3.backgroundColor=[UIColor clearColor];
    textField3.delegate=self;
    textField3.borderStyle=UITextBorderStyleRoundedRect;
    textField3.layer.borderColor=[UIColor blackColor].CGColor;
    textField3.font=[UIFont systemFontOfSize:14.0];
    textField3.keyboardType=UIKeyboardTypeNumberPad;
    textField3.returnKeyType=UIReturnKeyDone;
    textField3.backgroundColor=[UIColor whiteColor];
    [bgView addSubview:textField3];
    SiliwenTextView=[[[UITextView alloc]initWithFrame:CGRectMake(20, 236, 266, 165)]autorelease];
    SiliwenTextView.userInteractionEnabled=NO;
    SiliwenTextView.font=[UIFont systemFontOfSize:14.0];
    SiliwenTextView.textAlignment=NSTextAlignmentLeft;
    [bgView addSubview:SiliwenTextView];
    
    ShuoMing_View=[[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_W, IPHONE_H)]autorelease];
    
    UITextView* shuomingTextView=[[[UITextView alloc]initWithFrame:CGRectMake(0, 80, IPHONE_W, 487)]autorelease];
    shuomingTextView.userInteractionEnabled=NO;
    shuomingTextView.text=[NSString stringWithFormat:@"1、移动充值卡\n此卡为中国移动公司发行的神州行全国卡，卡号17位，密码18位。\n\n2、联通充值卡\n此卡为中国联通公司发行的联通一卡通卡，卡号15位，密码19位。\n\n3、电信充值卡\n此卡为中国电信公司发行，卡号19位，密码18位，卡号的第4位为1。\n\n4.%@充值卡\n此卡为%@独家发行，只能为%@账号充值，支持任意长度的卡号和密码。\n\n注：所有卡输入成功后，请等待3-5分钟后查询余额。充值无异议后，请再丢弃充值卡。",SOFT_NAME,SOFT_NAME,SOFT_NAME];
    shuomingTextView.font=[UIFont systemFontOfSize:13.0];
    shuomingTextView.textAlignment=NSTextAlignmentLeft;
    [ShuoMing_View addSubview:shuomingTextView];

    
    NSString *Value = [NSString stringWithFormat:@"此卡为%@发行，卡号%d位，密码%d位。所填金额必须与充值卡面额一致，否则由此带来的损失，本公司概不负责。",Name,KH_Length,MM_Length];
    
    if(ChongZhi_Type == 2){
        self.title = [NSString stringWithFormat:@"%@卡充值",SOFT_NAME];
        Value =[NSString stringWithFormat:@"此卡为%@独家发行，只能为%@账户充值。支持任意长度的卡号和密码。充值无异议后，再将充值卡丢弃。",SOFT_NAME,SOFT_NAME];
    }
    
    
    
    

    [SiliwenTextView setText:Value];
    
    
    
    if(ChongZhi_Type == 4){
        [self.view addSubview:ShuoMing_View];
        self.title = @"充值说明";
    }else{
        
        UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc]initWithTitle:@"充值"
                                                                       style:UIBarButtonItemStylePlain
                                                                      target:self action:@selector(ChongZhi)]autorelease];
        self.navigationItem.rightBarButtonItem = rightButton;
    }

    

    
    
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if(textField == textField2|| textField == textField3){
        
        [bgScroll setContentSize:CGSizeMake(320, 600)];
    }else{
        
        [bgScroll setContentSize:CGSizeMake(320, 480)];
        
        
    }
    
}





-(void)ChongZhi
{
    [textField3 resignFirstResponder];
    [textField2 resignFirstResponder];
    [textField1 resignFirstResponder];
    
    
    if(![[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_NUMBER"]){
        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提醒" message:@"请先绑定账户在操作." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确  定", nil];
        [alert show];
        [alert release];
        alert = nil;
        return;
    }
    
    HUD = [[[MBProgressHUD alloc] initWithView:self.view]autorelease];
    HUD.removeFromSuperViewOnHide = YES;
    [self.view addSubview:HUD];
    [HUD setLabelText:@"正在充值,请稍候..."];
    
    [HUD show:YES];
    
    
    
    
    
 
    
    
    NSString *Number = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_NUMBER"]];
    
    NSString* _priMD5=[NSString stringWithFormat:@"%@%@%@%@",Number,SOFT_ID,textField2.text,@"ysw"];
    NSString* MD5Str=[GlobalVar md5Str:_priMD5];
    
    
    ASIFormDataRequest* request;
    if ([GlobalVar sharedInstance].blnUseProxy==YES)
    {
        request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",origin_url_prefix,@"pay.php"]]];
        [request setProxyPort:ProxyPort];
        [request setProxyHost:[GlobalVar getTheBestIp]];//
    }else
    {
        request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[GlobalVar getTheBestUrl],@"proxy.php"]]];
        [request setPostValue:origin_url_prefix forKey:@"apiurl"];
        [request setPostValue:@"pay" forKey:@"act"];
    }
    [request setDelegate:self];
    [request setTimeOutSeconds:30.0f];
    [request setPostValue:Number forKey:@"account"];
    [request setPostValue:textField2.text forKey:@"password"];
    [request setPostValue:textField1.text forKey:@"pin"];
    [request setPostValue:SOFT_ID forKey:@"softid"];
    [request setPostValue:@"iphone" forKey:@"platform"];
    [request setPostValue:[NSString stringWithFormat:@"%d",ChongZhi_Type+1] forKey:@"type"];
    [request setPostValue:textField3.text forKey:@"money"];
    [request setPostValue:MD5Str forKey:@"md5"];
    [request setPostValue:MULTIAGENTSWITCH forKey:@"multiAgent"];
    [request setPostValue:NETWORKSTATE forKey:@"apn"];
    [request setPostValue:SOFT_VER forKey:@"version"];
    [request setDidFinishSelector:@selector(chongzhiSuccess:)];
    [request setDidFailSelector:@selector(chongzhiFail:)];
    [request startAsynchronous];
    
}


//充值回调
-(void)chongzhiSuccess:(ASIHTTPRequest*)request
{
    
    
    [HUD hide:YES];
    NSString* result=[request responseString];
    
    if ([result length]>0) {
        NSLog(@"充值的:%@",result);
        
        
        
        NSArray* comArr=[result componentsSeparatedByString:@"|"];
        if ([comArr count]>0) {
            
            NSString* code=[comArr objectAtIndex:0];
            if ([code isEqualToString:@"1"]) {
                
                iToast* toast=[iToast makeText:@"充值成功"];
                [toast show];
                
            }else
            {
                NSString* errorMsg=[comArr objectAtIndex:1];
                iToast* toast=[iToast makeText:errorMsg];
                [toast show];
            }
            
        }

    }
    
}

-(void)chongzhiFail:(ASIHTTPRequest*)request
{
    [HUD hide:YES];
    iToast* toast=[iToast makeText:ERRORCODE];
    [toast show];
    
   
    
    
}








-(void)GetBack{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    
    [super dealloc];
}
@end
